package com.zhhtech.zhh.ui.activity.main

import android.view.View
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.bean.HomeCaseBean
import com.zhhtech.zhh.databinding.ActivityRefreshBinding
import com.zhhtech.zhh.databinding.ItemCaseShareBinding
import com.zhhtech.zhh.ui.base.BaseActivity
import com.zhhtech.zhh.ui.base.BaseRecyclerAdapter
import com.zhhtech.zhh.ui.ext.startActivityExt
import com.zhhtech.zhh.ui.helper.RefreshHelper
import com.zhhtech.zhh.ui.utils.GlideUtils

class CaseShareActivity : BaseActivity<ActivityRefreshBinding>() {

    private lateinit var mRefreshHelper: RefreshHelper<HomeCaseBean>
    private val mAdapter = CaseShareAdapter()
    override fun initView() {
        vb.titleBar.titleStr = "案例分享"
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.rvList, mAdapter, listener = ::onReload)
        mRefreshHelper.onRefresh()
    }

    private fun onReload() {
        launchUI({ api.caseList(mRefreshHelper.page).toData()?.list }, mRefreshHelper.handApiObserver())
    }
}

class CaseShareAdapter : BaseRecyclerAdapter<ItemCaseShareBinding, HomeCaseBean>() {
    override fun onBindViewHolder(vb: ItemCaseShareBinding, bean: HomeCaseBean, position: Int) {
        GlideUtils.intoView(vb.ivImage.context, bean.imageUrl, vb.ivImage)
        vb.tvTitle.text = bean.title
        vb.tvContent.text = bean.content
        setViewClick(position, vb.root)
    }

    override fun subOnClickAdapter(v: View, position: Int) {
        v.context.startActivityExt<CaseDetailActivity> {
            putExtra("case", getItem(position))
        }
    }
}